import controlP5.*;

ControlP5 controlP5;
DropdownList p1, p2;
int cnt = 0;

static final String[] displayTypes = { 
  "Trends", "Map", "Graph"
};
static final String[] trendMonths = { 
  "June 2009", "July 2009", "August 2009", "September 2009", "October 2009", "November 2009", "December 2009"
};
static final String[] mapMonths = { 
  "Live", "November 2010"
};
static final String[] graphMonths = { 
  "June 2009", "July 2009", "August 2009", "September 2009", "October 2009", "November 2009", "December 2009"
};
String[] currentMonthArray;



// ControlP5 coordinates
int controlX=25;
int controlY=25;

// boolean values to keep track of with display type to show
boolean showTrends;
boolean showMap;
boolean showGraph;


void setup() {
  size(1024, 630);

  // set booleans (default is trends graph)
  showTrends = true;
  showMap = false;
  showGraph = false;

  // setup trends
  setupTrends();

  // setup map
  setupMap();

  // setup graph
  setupGraph();

  // create drop down list
  dropDownListSetup();
  setupRadioButton();
  setupGraphDDL();
}

void draw() {
  background(128);

  if (showTrends) {
    drawTrends();
  } 
  else if (showMap) {
    drawMap();
  } 
  else {
    drawGraph();
  }
}

void dropDownListSetup() {
  controlP5 = new ControlP5(this);

  currentMonthArray = trendMonths;

  p1 = controlP5.addDropdownList("myList-p1",controlX,controlY,100,120);
  customize(p1, "Display Type", displayTypes);

  p2 = controlP5.addDropdownList("myList-p2",controlX+120,controlY,100,120);
  customize(p2, "Time", currentMonthArray);
}

void customize(DropdownList ddl, String name, String[] items) {
  ddl.setBackgroundColor(color(190));
  ddl.setHeight(300);
  ddl.setItemHeight(30);
  ddl.setBarHeight(15);
  ddl.captionLabel().set(name);
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;
  for(int i=0;i<items.length;i++) {
    ddl.addItem(items[i],i);
  }
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255,128));
}

void controlEvent(ControlEvent theEvent) {

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    println(theEvent.group().value()+" from "+theEvent.group());
    if (theEvent.group().name().equals("myList-p1")) {
      // change the current month array based off of which display type was selcted
      switch ((int)theEvent.group().value()) {
      case 0:
        showTrends();
        p1.setPosition(controlX,controlY);
        p2.setPosition(controlX+120,controlY);
        break;
      case 1:
        showMaps();
        p1.setPosition(700,controlY);
        p2.setPosition(820,controlY);
        break;
      case 2:
        showGraph();
        p1.setPosition(controlX,controlY);
        p2.setPosition(controlX+120,controlY);
        break;
      }

      // refresh the month list
      refreshMonthList();
    } 
    else if (theEvent.group().name().equals("myList-p2")) {
      Cur_month = (int)theEvent.group().value() + 6;

      if (showGraph) {
        if (usersSelected) {
          usersChecked();
        }
        else {
          hashChecked();
        }
      }
      if (showMap) {
          //streamChoice = (int)theEvent.group().value() + 1;
      }
    }
    else if (theEvent.group().name().equals("graphList")) {
      graphValue = (int)theEvent.group().value() + 0;

      if (usersSelected) {
        userClicked();
      }
      else {
        hashClicked();
      }
    }
    else if (theEvent.group().name().equals("radioButton")) {
      print("got an event from "+theEvent.group().name()+"\t");

      for(int i=0;i<theEvent.group().arrayValue().length;i++) {
        print(int(theEvent.group().arrayValue()[i]));

        // check to see which radiobutton was selected and take the
        // appropriate action
        if (int(theEvent.group().value()) == 1.0) {
          usersChecked();
        }
        else if (int(theEvent.group().value()) == 2.0) {
          hashChecked();
        }
      }

      println("\t "+theEvent.group().value());
    }
  } 
  else if(theEvent.isController()) {
    println(theEvent.controller().value()+" from "+theEvent.controller());
  }
}

void refreshMonthList() {
  // clear the items currently in the drop down box
  p2.clear();

  // populate the drop down box with the items in current month array
  for(int i=0;i<currentMonthArray.length;i++) {
    p2.addItem(currentMonthArray[i],i);
  }

  // change the label back to time
  p2.captionLabel().set(currentMonthArray[0]);
}

void showTrends() {
  // reset the selected month
  Cur_month = 6;
  currentMonthArray = trendMonths;
  showTrends = true;
  showMap = false;
  showGraph = false;
  r.hide();
  graphList.hide();
}

void showMaps() {
  currentMonthArray = mapMonths;
  showTrends = false;
  showMap = true;
  showGraph = false;
  r.hide();
  graphList.hide();
}

void showGraph() {
  currentMonthArray = graphMonths;
  showTrends = false;
  showMap = false;
  showGraph = true;
  r.show();
  graphList.show();
}

